const CompressionPlugin = require('compression-webpack-plugin');
const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
const isProd = process.env.NODE_ENV === 'production'
const cdn = {
  externals: {
    vue: 'Vue',
    axios: 'axios',
    'vue-router': 'VueRouter',
    vuex: 'Vuex'
  },
  css: [],
  js: [
    '//cdn.jsdelivr.net/npm/vue@2.6.10/dist/vue.min.js',
    '//cdn.jsdelivr.net/npm/vue-router@3.1.3/dist/vue-router.min.js',
    '//cdn.jsdelivr.net/npm/vuex@3.1.1/dist/vuex.min.js',
    '//cdn.jsdelivr.net/npm/axios@0.19.0/dist/axios.min.js'
  ]
}
module.exports = {
  publicPath: "/",
  outputDir: "dist",
  assetsDir: "",
  indexPath: "index.html",
  lintOnSave: false,
  transpileDependencies: [],
  productionSourceMap: false,
  devServer: {
    open: true,
    port: 8889,
    https: false,
    proxy: {
      [process.env.VUE_APP_CONTEXT_PATH]: {
        target: "http://127.0.0.1:9050",
        // target: "http://119.23.31.32",
        changeOrigin: true,
        ws: false,
        pathRewrite: {
          ['^' + process.env.VUE_APP_CONTEXT_PATH]: [process.env.VUE_APP_CONTEXT_PATH]
        }
      }
    }
  },
  configureWebpack: {
    externals: isProd ? cdn.externals : {},
    optimization: {
      minimizer: [
        new UglifyJsPlugin({
          uglifyOptions: {
            compress: {
              drop_console: false,
              drop_debugger: false,
              pure_funcs: ['console.log']
            }
          }
        })
      ]
    },
    plugins: [
      new CompressionPlugin({
          test: /\.js$|\.html$|\.css/, // 匹配文件
          minRatio: 0.8,
          deleteOriginalAssets: false,
          threshold: 10240 // 对超过10k文件压缩
        },
      )
    ]
  },
  chainWebpack: config => {
    if (isProd) {
      config.plugin('html').tap(args => {
        args[0].cdn = cdn
        return args
      })
    }
  },
  css: {
    loaderOptions: {
      less: {
        lessOptions: {
          javascriptEnabled: true
        }
      }
    }
  }
};
